#!/usr/bin/env python

# (c) 2007-2011 Helsinki University of Technology and University of Helsinki
# Licensed under the MIT license; see LICENSE.txt for more information.

from harava.worker import Worker
from harava.rdf import *
from harava.validate import Validator

import re
import urllib2
import sys


class SUBIEventValidator (Validator):
  def __init__(self, session):
    Validator.__init__(self, session)

  def process(self, doc):
    # check required fields
    success = True

    # Summary (Title)
    success = success and self.check_required(doc, ICAL.summary, 'ical:summary', True)
    
    # Description
    success = success and self.check_required(doc, ICAL.description, 'ical:description', True, warnonly=True)
    
    # Start time
    if self.check_required(doc, ICAL.dtstart, 'ical:dtstart', True):
      value = doc.getMetadata(ICAL.dtstart, True)
      valid = self.validate_w3cdtf(value)
      if not valid:
        doc.log('CRIT', 'validate', 'Invalid value for ical:dtstart', value)
        success = False
    else:
      success = False
    
    # End time
    if self.check_required(doc, ICAL.dtend, 'ical:dtend', True):
      value = doc.getMetadata(ICAL.dtend, True)
      valid = self.validate_w3cdtf(value)
      if not valid:
        doc.log('CRIT', 'validate', 'Invalid value for ical:dtend', value)
        success = False
    else:
      success = False
    
    # location stuff
    success = success and self.check_required(doc, ICAL.location, 'ical:location', True)

    loc = doc.getMetadata(ICAL.location, True)
    graph = doc.getMetadataGraph()

    if loc is not None:
      # Address object inside vCard
      # TODO: check for multiple addresses and pick one if there are several
      adr = graph.value(loc, VCARD.adr, None)
      if adr is None:
        doc.log('CRIT', 'validate', 'Location address missing')
        success = False
      else:
        # TODO: check for multiple street addresses

        locality = graph.value(adr, VCARD.locality, None)
        if locality is None:
          doc.log('CRIT', 'validate', 'Location municipality/locality missing')
          success = False
        # TODO: validate the locality with ONKI
        
        country = graph.value(adr, VCARD['country-name'], None)
        # TODO: check for multiple countries
        if country is None:
          doc.log('NOTE', 'validate', 'Location country missing, assuming Finland')
          graph.add((adr, VCARD['country-name'], Literal('Finland', 'en')))
    
    # Subject
# check disabled while waiting for ARPA
#    concs = doc.getMetadata(DCTERMS.subject)
#    if concs:
#      for conc in concs:
#        # TODO also validate concept scheme
#        if not self.validate_concept(conc, SKOS.Concept):
#          doc.log('WARN', 'validate', 'Subject concept validation failed', conc)
#          doc.clearMetadata(DCTERMS.subject, conc)
#    success = success and self.check_required(doc, DCTERMS.subject, 'dcterms:subject')

    if not success:
      doc.processingFinished()
      return False

    
    doc.setCompleted('validate')
    self._session.schedule(doc)
    return True

  def finish(self):
    # possibly free some memory
    Validator.finish(self)
